using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;
using System.Collections.Generic;





namespace FShop.DAL
{

    public class BasketDAL:BaseObject
    {

        private List<OrderProduct> products;
        public List<OrderProduct> ListProduct
        {
            get
            {
                return products;
            }
        }

        private int count = 0;
        

        public int Count
        {
            get { return count; }
        }
        public  float Summary
        {
            get
            {              

                float temp = 0;

                foreach (OrderProduct o in products)
                {
                    temp += o.Summary;
                }
                return temp;
            }
        }


        public OrderProduct GetProductFromBasket(string _productID)
        {
            OrderProduct pro = new OrderProduct();
            pro.OrderProductID = _productID;

            int index = CheckProductExit(pro);
            if (index == -1)
                return null;
            else
                return products[index];



        }
        public BasketDAL()
        {
            products = new List<OrderProduct>();
        }

        public  void InsertProductToBasket(OrderProduct _product)
        {
            int index=CheckProductExit(_product);

            if (index == -1)//Not exit
            {
                products.Add(_product);
            }
            else//Exit then amount of this item increase
            {
                products[index].Amount += 1;
            }

            //total items in basket
            count += 1;
        }

        private int CheckProductExit(OrderProduct _product)
        {
            for (int i = 0; i < products.Count; i++)
                if (products[i].OrderProductID == _product.OrderProductID)
                    return i;          

            return -1;
        }
   
        public void DeleteProductFromBasket(OrderProduct _product)
        {
            int index = CheckProductExit(_product);
            if (index >= 0)
            //if (products.Contains(_product))
            {
                if (products[index].Amount > 1)
                    products[index].Amount = -1;
                else
                    products.RemoveAt(index);
                count -= 1;
            }
        }
        public void UpdateProductFromBasket(OrderProduct _oldProduct, OrderProduct _newProduct)
        {           
            int index = products.IndexOf(_oldProduct);

            if (index >= 0)
                products[index] = _newProduct;
            float i = this.Summary;
            i = products[index].Summary;
            ////reset total amount

            int temp = 0;

            foreach (OrderProduct o in products)
            {
                temp += o.Amount;
            }
            this.count = temp;
        }

        public void InsertProductIntoTable(string basketID)
        {
            using (SqlConnection conn = CreateConnection())
            {
                conn.Open();
                SqlTransaction myTrans = conn.BeginTransaction();
                SqlCommand cmd = null;
                try
                {

                    foreach (OrderProduct o in products)
                    {
                        cmd = new SqlCommand("usp_Basket_Insert", conn);
                        cmd.Transaction = myTrans;
                        cmd.CommandType = CommandType.StoredProcedure;
                        //cmd.Parameters.Add(new SqlParameter("@giohangid", hoadonid));
                        cmd.Parameters.Add(new SqlParameter("@Pro_ID", o.OrderProductID));
                        cmd.Parameters.Add(new SqlParameter("@Pro_Name", o.ProName));
                        cmd.Parameters.Add(new SqlParameter("@Pro_Amount", o.Amount));
                        cmd.Parameters.Add(new SqlParameter("@Pro_Price", o.Price));
                        cmd.Parameters.Add(new SqlParameter("@Basket_ID", basketID));
                        cmd.ExecuteNonQuery();
                    }
                    //nd

                    myTrans.Commit();
                    conn.Close();
                }
                catch
                {
                    myTrans.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public DataTable GetProductFromDatabase(string basketID)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Basket where Basket.Basket_ID=@Backet_ID", conn);
                cmd.CommandType = CommandType.Text;       
                cmd.Parameters.Add(new SqlParameter("@Backet_ID",basketID));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    DataTable tbl = new DataTable();
                    dataAdapter.Fill(tbl);
                    conn.Close();
                    return tbl;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }
        
    }

}
